#include"StarLayer.h"
#include<random>
using namespace std;
USING_NS_CC;

bool StarLayer::init()
{
	if (Layer::init())
	{
		auto touchListener = EventListenerTouchOneByOne::create();
		touchListener->onTouchBegan = CC_CALLBACK_2(StarLayer::onTouchBegan, this);
		touchListener->onTouchMoved = CC_CALLBACK_2(StarLayer::onTouchMoved, this);
		touchListener->onTouchEnded = CC_CALLBACK_2(StarLayer::onTouchEnded , this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

		cocos2d::Size backSize = Director::getInstance()->getWinSize();
		CCTexture2D* textture = CCTextureCache::sharedTextureCache()->addImage("star.png");
		float w = textture->getContentSize().width;
		float h = textture->getContentSize().height / 6;
		CCAnimation* ani = CCAnimation::create();
		ani->setDelayPerUnit(0.15f);
		for (int i = 0; i < 6; ++i)
		{
			ani->addSpriteFrameWithTexture(textture, CCRectMake(0, i*h, w, h));
		}
		CCAnimate* animate = CCAnimate::create(ani);
		m_star = CCSprite::create();
		m_star->setScale(0.5f);
		m_star->runAction(CCRepeatForever::create(animate));
		m_star->setPosition(backSize.width*0.5, backSize.height*0.5);
		this->addChild(m_star);

		m_Cannon = Cannon::create();
		m_Cannon->setPosition(ccp(backSize.width*0.5, 15));
		m_Cannon->setScale(0.5f);
		this->addChild(m_Cannon);

	/*	CCSpriteBatchNode* SpriteNode = CCSpriteBatchNode::create("tri.png");
		SpriteNode->setPosition(ccp(0, 0));
		this->addChild(SpriteNode);
		for (int i = 0; i < 30; ++i)
		{
			CCAnimate* animate2 = CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName("triangle"));
			CCSprite* triangle = CCSprite::createWithTexture(SpriteNode->getTexture());
			random_device rad;
			float x = rad() % static_cast<int>(backSize.width);
			float y = rad() % static_cast<int>(backSize.height);
			triangle->runAction(CCRepeatForever::create(animate2));
			triangle->setPosition(x, y);
			SpriteNode->addChild(triangle);
		}*/
		
	/*	CCSpriteBatchNode* SpriteNode = CCSpriteBatchNode::create("door.png");
		this->addChild(SpriteNode);
		for (int i = 0; i < 30; ++i)
		{
			CCSprite* door = CCSprite::createWithTexture(SpriteNode->getTexture());
			float w = CCRANDOM_0_1()*backSize.width;
			float h = CCRANDOM_0_1()*backSize.height;
			door->setPosition(w, h);
			SpriteNode->addChild(door);
		}*/

		/*CCMenuItemImage* pButtonThree = CCMenuItemImage::create("btn.png",
			"btn.png",
			this,
			menu_selector(StarLayer::CallButtonThreeBack));
		pButtonThree->setPosition(270, 30);

		CCMenu* pMenu = CCMenu::create(pButtonThree, nullptr);
		pMenu->setPosition(CCPointMake(0, 0));
		this->addChild(pMenu);*/
		/*this->schedule(schedule_selector(StarLayer::MoveStar));*/
		return true;
	}
	return false;
}

void StarLayer::MoveStar(float dt)
{
	m_star->setPositionX(m_star->getPositionX() - 1);
	if (m_star->getPositionX() < 0 - m_star->getContentSize().width*0.5)
	{
		m_star->setPositionX(Director::getInstance()->getWinSize().width + m_star->getContentSize().width*0.5);
	}
}

void StarLayer::CallButtonThreeBack(cocos2d::CCObject * sender)
{
	CCActionInterval* ai = nullptr;
	
	/*ai = CCMoveTo::create(2.0f, ccp(100, 100));
	m_star->runAction(ai);
*/
	/*ai = CCMoveBy::create(2.0f, ccp(50, 50));
	m_star->runAction(ai);*/

	//ai = CCScaleTo::create(2.0f, 1.5f);
	//m_star->runAction(ai);

	/*ai = CCScaleBy::create(2.0f, 1.2f);
	m_star->runAction(ai);*/

	//ai = CCRotateBy::create(2.0f, 90.0f);
	//m_star->runAction(ai);

	/*ai = CCJumpBy::create(2.0f, ccp(100, 150), 20, 3);
	m_star->runAction(ai);*/

	/*ai = CCFadeOut::create(2);
	m_star->runAction(ai);*/

	//ai = CCBlink::create(2, 10);
	//m_star->runAction(ai);

	//ai = CCTintTo::create(2, 0, 255, 0);
	//m_star->runAction(ai);

	//CCFiniteTimeAction* fta = CCFlipX::create(true);
	//m_star->runAction(fta);

	/*ccBezierConfig bezierConfig;
	bezierConfig.controlPoint_1 = ccp(100, 30);
	bezierConfig.controlPoint_2 = ccp(200, 160);
	bezierConfig.endPosition = ccp(150, 100);
	ai = BezierTo::create(2, bezierConfig);
	m_star->runAction(ai);*/

	CCActionInterval* move1 = CCMoveTo::create(3, ccp(-180, 160));
	CCFiniteTimeAction* flipRight = CCFlipX::create(true);
	CCActionInterval* move2 = CCMoveTo::create(5, ccp(620, 160));
	CCFiniteTimeAction* flipLeft = CCFlipX::create(false);
	CCActionInterval* move3 = CCMoveTo::create(3, ccp(240, 160));
	CCAction* action = CCRepeatForever::create(CCSequence::create(move1, flipRight, move2, flipLeft, move3, nullptr));
	m_star->runAction(action);
}

cocos2d::CCPoint StarLayer::loactionFromTouch(cocos2d::Touch * touch)
{
	return CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
}

bool StarLayer::onTouchBegan(cocos2d::Touch * touch, cocos2d::Event * event)
{
	/*CCPoint location = loactionFromTouch(touch);
	CCFiniteTimeAction* fta = CCPlace::create(location);
	m_star->runAction(fta);*/
	m_Cannon->setDrawLine(true);
	CCPoint targetTouch = loactionFromTouch(touch);
	CCPoint targetY = ccp(m_Cannon->getPosition().x, 1024);
	float angle = ccpAngleSigned(ccpSub(targetTouch,
		m_Cannon->getPosition()),
		ccpSub(targetY, m_Cannon->getPosition()));
	m_Cannon->setRotation(CC_RADIANS_TO_DEGREES(angle));
	return true;
}

void StarLayer::onTouchMoved(cocos2d::Touch * touch, cocos2d::Event * event)
{
	/*CCPoint location = loactionFromTouch(touch);
	CCFiniteTimeAction* fta = CCPlace::create(location);
	m_star->runAction(fta);*/
	m_Cannon->setDrawLine(true);
	CCPoint targetTouch = loactionFromTouch(touch);
	CCPoint targetY = ccp(m_Cannon->getPosition().x, 1024);
	float angle = ccpAngleSigned(ccpSub(targetTouch,
		m_Cannon->getPosition()),
		ccpSub(targetY, m_Cannon->getPosition()));
	m_Cannon->setRotation(CC_RADIANS_TO_DEGREES(angle));
}

void StarLayer::onTouchEnded(cocos2d::Touch * touch, cocos2d::Event * event)
{
	//CCPoint location = loactionFromTouch(touch);
	//CCFiniteTimeAction* fta = CCPlace::create(location);
	//m_star->runAction(fta);
	CCPoint targetTouch = loactionFromTouch(touch);
	m_Cannon->setDrawLine(false);
	CCPoint targetY = ccp(m_Cannon->getPosition().x, 1024);
	float angle = ccpAngleSigned(ccpSub(targetTouch,
		m_Cannon->getPosition()),
		ccpSub(targetY, m_Cannon->getPosition()));
	m_Cannon->setRotation(CC_RADIANS_TO_DEGREES(angle));
}
